package com.itheima.d02;

import java.io.IOException;
import java.net.Socket;
import java.util.Objects;

public class ChatRunnable implements Runnable {
    private Socket socket;

    public ChatRunnable(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        try (java.io.InputStream is = socket.getInputStream();
             java.io.BufferedReader br = new java.io.BufferedReader(new java.io.InputStreamReader(is));) {
            // 1. 获取输入流，接收数据


            while (true) {
                String msg = br.readLine();
                System.out.println("接收客户端的信息" + msg);

                if (Objects.equals(msg, "bye")) {
                    System.out.println(socket.getRemoteSocketAddress() + "客户强制推出");
                    break;
                }
            }
        } catch (Exception e) {
            System.out.println(socket.getRemoteSocketAddress() + "客户退出");
        }
    }
}
